package com.dm.dao;

import java.io.*;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *CREATE TABLE "SYSDBA"."PRODUCT_CATEGORY"
 * (
 * "PRODUCT_CATEGORYID" INT IDENTITY(1, 1) NOT NULL,
 * "NAME" VARCHAR(50) NOT NULL,
 * CLUSTER PRIMARY KEY("PRODUCT_CATEGORYID")) STORAGE(ON "BOOKSHOP", CLUSTERBTR) ;
 */
public class JdbcCon {
    static List<String> strList = new ArrayList<>();

    //数据库连接对象
    public static Connection getConnection(String sqlConnStr) {

        String username = sqlConnStr.split("/")[0];
        String password = sqlConnStr.split("/")[1].split("@")[0];
        String connStr = sqlConnStr.split("/")[1].split("@")[1].split(":")[0];

        System.out.println(username);
        System.out.println(password);
        System.out.println(connStr);

        String driver = "dm.jdbc.driver.DmDriver";

        String url = "jdbc:dm://" + connStr;

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public static void closeConnection(Connection connection) {

        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
       //SYSDBA/SYSDBA@127.0.0.1:5236

       // System.out.println(args.toString());
     //   String ConnectStr = "SYSDBA/SYSDBA@127.0.0.1:5236";
        //java -jar java_dm.jar SYSDBA/SYSDBA@DM
        String sqlConnStr = args[0];


        Connection connection = getConnection(sqlConnStr);
        if (connection != null) {
            System.out.println("数据库连接成功");
        } else {
            System.out.println("数据库连接失败");
        }
        Statement stmt = null;
        try {
            for (int i = 0; i < 100; i++) {
                String query_sql = "select * from PRODUCT_CATEGORY";

                stmt = connection.createStatement();
                ResultSet rs1 = stmt.executeQuery(query_sql);
                while (rs1.next()) {
                    System.out.println(
                            rs1.getInt(1) + "," +
                                    rs1.getString(2)
                    );
                }

                Thread.sleep(2000);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        closeConnection(connection);
    }

}
